// <editor-fold defaultstate="collapsed" desc="GNU GPLv3 Header">
/*
 * File:   Main.java
 *
 * Copyright (C) 2013 Robert Antoni Buj Gelonch <rbuj@uoc.edu>
 * Copyright (C) 2013 David Megias Jimenez <dmegias@uoc.edu>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
// </editor-fold>
package nonpcmulawconverttopcmshort;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import uoc.libcompression.G711.UlawDecoder;
import uoc.libwave.WaveFormatNonPCMByteMlaw;
import uoc.libwave.WaveFormatPCMShort;

/**
 *
 * @author Robert Antoni Buj Gelonch <rbuj@uoc.edu>
 * @version 1.0
 * @since 2013-09-02
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        if (args.length < 2) {
            System.err.println("Must supply filenames:");
            System.err.println("java -jar NonPCM-Ulaw-ConvertToPCMShort.jar"
                    + " input.wav output.wav");
            System.exit(1);
        }

        String strSrcFilePath = args[0];
        String strDstFilePath = args[1];

        try {
            // Load the wave file
            WaveFormatNonPCMByteMlaw waveData =
                    new WaveFormatNonPCMByteMlaw(strSrcFilePath);

            // Variables initalization
            UlawDecoder ulawDecoder = new UlawDecoder();
            byte ulawSample;
            short wChannels = waveData.getWChannel();
            int lenght = waveData.getNumSamples();
            int dwSamplesPerSec = waveData.getDwSamplesPerSec();
            short[][] expanded = new short[wChannels][lenght];
            short res;

            for (short i = 0; i < wChannels; i++) {
                for (int j = 0; j < lenght; j++) {
                    ulawSample = waveData.getSample((short) (i + 1), j);
                    // get the compressed value
                    res = ulawDecoder.expandTransmission(ulawSample);
                    expanded[i][j] = (short) (res * 4);
                }
            }

            WaveFormatPCMShort PcmWaveData  = new WaveFormatPCMShort(expanded,
                    lenght, wChannels, dwSamplesPerSec);
            PcmWaveData.saveToFile(strDstFilePath);

        } catch (FileNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
